package cn.zd.demo1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

public class SelectorActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener, RadioGroup.OnCheckedChangeListener {

    private TextView textView;
    private TextView tv_sw;
    private TextView tv_rg_default;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Toast.makeText(this,this.getClass().getName(),Toast.LENGTH_LONG).show();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selector);
        RadioGroup rg_default = findViewById(R.id.rg_default);
        tv_rg_default = findViewById(R.id.tv_rg_default);
        textView = findViewById(R.id.tv_info);
        tv_sw = findViewById(R.id.tv_sw_info);
        CheckBox ck_default = findViewById(R.id.ck_default);
        CheckBox ck_img = findViewById(R.id.ck_img);
        ck_default.setOnCheckedChangeListener(this);
        ck_img.setOnCheckedChangeListener(this);
        rg_default.setOnCheckedChangeListener(this);
        Switch sw_default = findViewById(R.id.sw_default);
        sw_default.setOnCheckedChangeListener(this);

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (buttonView.getId() == R.id.ck_default || buttonView.getId() == R.id.ck_img) {
            textView.setText(String.format("刚刚点击了： %s,状态：%s", buttonView.getText(), isChecked ? "选中" : "未选中"));
        } else if (buttonView.getId() == R.id.sw_default) {
            tv_sw.setText(String.format("刚刚点击了： %s,状态：%s", buttonView.getText(), isChecked ? "选中" : "未选中"));

        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if(group.getId() == R.id.rg_default) {
            // 获取选中的radiobutton的id
            // group.getCheckedRadioButtonId()
            tv_rg_default.setText(String.format("%s单选选中的id是：%s", "系统默认单选", checkedId == R.id.rg_default_1 ? "单选1" : (checkedId == R.id.rg_default_2 ? "单选2" : "未知")));
        }
    }
}